package com.example.javademo;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.ArrayList;
import java.util.List;

public class BaseActivity extends AppCompatActivity {

    private ListView listView3;
    private ImageView write;
    private List<Msg> data = new ArrayList<>();
    private BaseAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_base);

        listView3 = findViewById(R.id.list_view3);
        write = findViewById(R.id.write);
        
        initData();
        adapter = new MyAdataer(data, this);
 
        listView3.setAdapter(adapter);

        write.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = data.size() +1;
                String s = "这是第"+i+"条消息";
                Msg msg = new Msg(R.drawable.today, "小王", s, false);
                data.add(msg);
                adapter.notifyDataSetChanged(); //通知数据集发生改变
                listView3.setSelection(data.size()-1); //滚动到最后一行
            }
        });
    }

    private void initData() {

        for(int i=0; i<=8; i++){
            data.add(new Msg(R.drawable.today, "小王"+ i, "这是第" + i + "条消息", false));
        }
    }
}